package sddz.foodmanage.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import sddz.foodmanage.utils.ImageLoader;


/**
 * Created by Administrator on 2017/11/19.
 */

public class ViewHolder extends RecyclerView.ViewHolder {

    private SparseArray<View> mViews;
    public View mConvertView;
    private Context mContext;

    public ViewHolder(Context context, View itemView, ViewGroup parent) {
        super(itemView);
        mContext = context;
        mConvertView = itemView;
        mViews = new SparseArray<View>();
    }


    public static ViewHolder get(Context context, ViewGroup parent, int layoutId) {

        View itemView = LayoutInflater.from(context).inflate(layoutId, parent,
                false);
        ViewHolder holder = new ViewHolder(context, itemView, parent);
        return holder;
    }


    /**
     * 通过viewId获取控件
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 为Text添加数据
     *
     * @param viewId
     * @param text
     */
    public void setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
    }


    /**
     * 为ImageView添加数据
     *
     * @param viewId
     */
    public void setImageFile(Activity activity, int viewId, String imageUrl) {
        ImageView view = getView(viewId);
        ImageLoader.loadImageFile(activity, imageUrl, view);
    }
    public void setImageFile2(Activity activity, int viewId, String imageUrl) {
        ImageView view = getView(viewId);
        ImageLoader.loadImageUrl1(activity, imageUrl, view);
    }
    public void setImageFile3(Activity activity, int viewId, String imageUrl) {
        ImageView view = getView(viewId);
        ImageLoader.loadImageUrl(activity, imageUrl, view);
    }
//    public void setImageurl(Activity activity, int viewId, String imageUrl) {
//        ImageView view = getView(viewId);
//        URL myFileURL;
//        Bitmap bitmap=null;
//        try{
//            myFileURL = new URL(imageUrl);
//            //获得连接
//            HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection();
//            //设置超时时间为6000毫秒，conn.setConnectionTiem(0);表示没有时间限制
//            conn.setConnectTimeout(6000);
//            //连接设置获得数据流
//            conn.setDoInput(true);
//            //不使用缓存
//            conn.setUseCaches(false);
//            //这句可有可无，没有影响
//            //conn.connect();
//            //得到数据流
//            InputStream is = conn.getInputStream();
//            //解析得到图片
//            bitmap = BitmapFactory.decodeStream(is);
//            //关闭数据流
//            is.close();
//        }catch(Exception e){
//            e.printStackTrace();
//        }
//        ImageLoader.loadImageURl(activity,bitmap,view);
//
//    }

}
